package com.jdls.tanhua.dubbo.impl;

import com.jdls.tanhua.dubbo.CommentApi;
import com.jdls.tanhua.mongo.Comment;
import com.jdls.tanhua.mongo.Movement;
import com.jdls.tanhua.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.checkerframework.checker.units.qual.C;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @author 嘉德罗斯
 * @date 2021/10/29
 */
@DubboService
public class CommentImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public Integer save(Comment comment) {
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Movement movement = mongoTemplate.findOne(query, Movement.class);
        Video video = mongoTemplate.findOne(query, Video.class);
        if (movement!=null){
            comment.setPublishUserId(movement.getUserId());
        }else if (video!=null){
            comment.setPublishUserId(video.getUserId());
        }
        //设置发布动态用户id
        //保存评论
        mongoTemplate.save(comment);
        Update update = new Update();
        if (comment.getCommentType() == 1){
            update.inc("likeCount",1);
        }else if(comment.getCommentType() == 2){
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }

        FindAndModifyOptions options = new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);


        if (movement!=null){
            Movement andModify = mongoTemplate.findAndModify(query, update, options, Movement.class);
            //获取最新互动的数据
            if (comment.getCommentType() == 1){
                return andModify.getLikeCount();
            }else if(comment.getCommentType() == 2){
                return andModify.getCommentCount();
            }else {
                return andModify.getLoveCount();
            }
        }else {
            Video video1 = mongoTemplate.findAndModify(query, update, options, Video.class);
            //获取最新互动的数据
            if (comment.getCommentType() == 1){
                return video1.getLikeCount();
            }else if(comment.getCommentType() == 2){
                return video1.getCommentCount();
            }else {
                return video1.getLoveCount();
            }
        }
    }

    @Override
    public List<Comment> findAll(ObjectId mid, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("publishId").is(mid))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((page-1)*pagesize)
                .limit(pagesize);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    @Override
    public Comment find(Long userId, ObjectId movementId, int like) {
        Criteria criteria = Criteria.where("publishId").is(movementId).and("commentType").is(like)
                .and("userId").is(userId);
        Query query = Query.query(criteria);
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        return comment;
    }

    @Override
    public Integer delete(Long userId, ObjectId movementId, int like) {
        Criteria criteria = Criteria.where("publishId").is(movementId).and("userId").is(userId)
                .and("commentType").is(like);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);

        Update update = new Update();
        if (like == 1){
            update.inc("likeCount",-1);
        }else if(like == 2){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }

        FindAndModifyOptions options = new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        Query q = Query.query(Criteria.where("id").is(movementId));
        Movement andModify = mongoTemplate.findAndModify(q, update, options, Movement.class);
        Video video = mongoTemplate.findAndModify(q, update, options, Video.class);
        if (andModify != null){
            if (like == 1){
                return andModify.getLikeCount();
            }else if(like == 2){
                return andModify.getCommentCount();
            }else {
                return andModify.getLoveCount();
            }
        }else {
            if (like == 1){
                return video.getLikeCount();
            }else if(like == 2){
                return video.getCommentCount();
            }else {
                return video.getLoveCount();
            }
        }

    }

    @Override
    //评论点赞
    public Integer like(ObjectId commentId) {
        Query q = Query.query(Criteria.where("id").is(commentId));
        Update update = new Update();
        update.inc("likeCount",1);
        FindAndModifyOptions options = new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        Comment andModify = mongoTemplate.findAndModify(q, update, options, Comment.class);
        if (andModify == null){
            return 0;
        }
        return andModify.getLikeCount();
    }

    @Override
    public Integer dislike(ObjectId commentId) {
        Query q = Query.query(Criteria.where("id").is(commentId));
        Update update = new Update();
        update.inc("likeCount",-1);
        FindAndModifyOptions options = new FindAndModifyOptions();
        //获取更新后的最新数据
        options.returnNew(true);
        Comment andModify = mongoTemplate.findAndModify(q, update, options, Comment.class);
        if (andModify == null){
            return 0;
        }
        return andModify.getLikeCount();
    }

    @Override
    public List<Comment> findLike(Long id, Integer page, Integer pagesize, int type) {
        Query query = Query.query(Criteria.where("userId").is(id).and("commentType").is(type))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((page-1)*pagesize).limit(pagesize);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }
}
